package com.wayeal.recheck.util;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author jian
 * @version 2021-08-16 11:38
 */
public class ReadUtil {
    private static final Logger LOGGER = LoggerFactory.getLogger(ReadUtil.class);

    public static void read(List<Map<Integer,String>> list){
        Map<Integer,String> row=new TreeMap<>();
        for (int i=0;i<list.size();i++){
            if (list.get(i).size()>0){
                //todo 可以提取待做
              if(list.get(i).containsValue(Constant.SO2)){
                  rowUtil(list.get(i),row,Constant.SO2,i);
               }
              if(list.get(i).containsValue(Constant.NO)){
                  rowUtil(list.get(i),row,Constant.NO,i);
              }
              if(list.get(i).containsValue(Constant.O2)){
                  rowUtil(list.get(i),row,Constant.O2,i);
              }
              if(list.get(i).containsValue(Constant.O2)){
                  rowUtil(list.get(i),row,Constant.O2,i);
              }
              if(list.get(i).containsValue(Constant.PARTICULATE_MATTER)){
                  rowUtil(list.get(i),row,Constant.PARTICULATE_MATTER,i);
              }
            }
          }
           int total=list.size();
           Map<String,List<Integer>> listMap= diff(row,total);
           getTableData(listMap,list);
        }

    public static void rowUtil(Map<Integer,String> source,Map<Integer,String> row,String factor,Integer point){
        for (Map.Entry<Integer,String> entry:source.entrySet()){
            if (factor.equals(entry.getValue())){
                row.put(point,factor);
            }
        }
    }
    public static Map<String,List<Integer>> diff(Map<Integer,String> row,int end){
        Map<String,List<Integer>> map=new LinkedHashMap<>();
        List<Integer> arr= new ArrayList(row.keySet());
        arr.add(end);
        int flag=arr.size()-1;
       for (int i=0;i<flag;i++){
           List<Integer> list=new ArrayList<>();
           if (i+1<=flag){
               list.add(arr.get(i));
               list.add(arr.get(i+1));
               map.put(row.get(arr.get(i)),list);
           }
       }
        return  map;
    }
    public static void getTableData(Map<String,List<Integer>> diff,List<Map<Integer,String>> row){
        List <String> colModel=new ArrayList<>();
        for (Map.Entry<String,List<Integer>> entry:diff.entrySet()){
            RecheckGasDrift recheckGasDrift=new RecheckGasDrift();
            if (entry.getValue().size()>=2){
                //do
               List<Map<Integer,String>> table=getTableDataByIndex(row,entry.getValue().get(0),entry.getValue().get(1));

               List<Integer> headDivision=tableDivision(table,entry.getKey(),RecheckConstant.Z0);
               if (headDivision.size()>=2){
                   List<Map<Integer,String>> head= getTableDataByIndex(table,headDivision.get(0),headDivision.get(1)+1);
                   getHeadData(head,colModel,recheckGasDrift,entry.getKey());
               }
                List<Integer> valueDivision=tableDivision(table,RecheckConstant.Z0,RecheckConstant.ZERO_DRIFT_ABSOLUTE_ERROR);
               if (valueDivision.size()>=2){
                   List<Map<Integer,String>>  value= getTableDataByIndex(table,valueDivision.get(0)+1,valueDivision.get(1));
                   getValueData(value,colModel,recheckGasDrift);
               }
               List<Integer> footDivision = tableDivision(table, RecheckConstant.ZERO_DRIFT_ABSOLUTE_ERROR);
               if (footDivision.size()>=2) {
                   List<Map<Integer,String>>  foot= getTableDataByIndex(table,footDivision.get(0),footDivision.get(1));
                   getFootData(foot,recheckGasDrift);
               }
                LOGGER.info("*********************************************");
                LOGGER.info(JSONObject.toJSONString(recheckGasDrift, SerializerFeature.WriteMapNullValue));
                LOGGER.info("*********************************************");

            }

        }

    }
    public static List<Map<Integer,String>> getTableDataByIndex(List<Map<Integer,String>> row,Integer form,Integer end){
            List<Map<Integer,String>> oneTable= row.subList(form,end);
            return oneTable;
    }
    public static void getDataFormMap(Map<Integer,String> data){

        for (Map.Entry<Integer,String> entry:data.entrySet()){

            if (entry.getValue().equals("")){

            }
        }
    }
    public static List<Integer> tableDivision( List<Map<Integer,String>> list,String startMark,String endMark){
        List<Integer> startAndEnd=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            for (Map.Entry<Integer,String> entry:list.get(i).entrySet()){
                String s=entry.getValue();
                if (s!=null){
                    if (s.indexOf(startMark)>-1){
                        startAndEnd.add(i);
                    }
                    if (s.indexOf(endMark)>-1){
                        startAndEnd.add(i);
                    }
                }

            }

        }
        return startAndEnd;
    }
    public static List<Integer> tableDivision( List<Map<Integer,String>> list,String startMark){
        List<Integer> startAndEnd=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            if (list.get(i).containsValue(startMark)){
                startAndEnd.add(i);
            }
        }
        startAndEnd.add(list.size());
        return startAndEnd;
    }
    public static void getHeadData(List<Map<Integer,String>> list,List<String> col,RecheckGasDrift recheckGasDrift,String name){
        RecheckGasHead recheckGasHead=new RecheckGasHead();
        recheckGasHead.setName(name);
        for (int i=0;i<list.size();i++){
            Map<Integer,String> entry= list.get(i);
            for (Integer key : entry.keySet()){
                if ( RecheckConstant.SPAN_GAS.equals(entry.get(key))){
                    if (key+2<entry.keySet().size()) {
                        recheckGasHead.setSpanGas(entry.get(key + 1));
                        recheckGasHead.setUnit(entry.get(key + 2));
                    }
                }
                if (RecheckConstant.RANG.equals(entry.get(key))){
                    if (key+1<entry.keySet().size()) {
                        recheckGasHead.setRange(entry.get(key + 1));
                    }
                }
                if (RecheckConstant.DATETIME.equals(entry.get(key))){
                    col.add(key,RecheckConstant.DATETIME);
                }
                if (RecheckConstant.Z0.equals(entry.get(key))){
                    col.add(key,RecheckConstant.Z0);
                }
                if (RecheckConstant.Zi.equals(entry.get(key))){
                    col.add(key,RecheckConstant.Zi);
                }
                if (RecheckConstant.Zd.equals(entry.get(key))){
                    col.add(key,RecheckConstant.Zd);
                }
                if (RecheckConstant.S0.equals(entry.get(key))){
                    col.add(key,RecheckConstant.S0);
                }
                if (RecheckConstant.Si.equals(entry.get(key))){
                    col.add(key,RecheckConstant.Si);
                }
                if (RecheckConstant.Sd.equals(entry.get(key))){
                    col.add(key,RecheckConstant.Sd);
                }
            }
        }
        recheckGasDrift.setRecheckGasHead(recheckGasHead);
    }
    public static void getValueData(List<Map<Integer,String>> list,List<String> col,RecheckGasDrift recheckGasDrift){
        List<RecheckGasValue> values=new ArrayList<>();
        for (int i=0;i<list.size();i++){
            Map<Integer,String> entry= list.get(i);
            RecheckGasValue recheckGasValue=new RecheckGasValue();
              for (Integer key : entry.keySet()){
                     if (RecheckConstant.DATETIME.equals(col.get(key))){
                         recheckGasValue.setDataTime(entry.get(key));
                     }
                     if (RecheckConstant.Z0.equals(col.get(key))){
                         recheckGasValue.setZ0(entry.get(key));
                     }
                     if (RecheckConstant.Zi.equals(col.get(key))){
                         recheckGasValue.setZi(entry.get(key));
                     }
                     if (RecheckConstant.Zd.equals(col.get(key))){
                         recheckGasValue.setDiffZ(entry.get(key));
                     }
                     if (RecheckConstant.S0.equals(col.get(key))){
                         recheckGasValue.setS0(entry.get(key));
                     }
                     if (RecheckConstant.Si.equals(col.get(key))){
                         recheckGasValue.setSi(entry.get(key));
                     }
                     if (RecheckConstant.Sd.equals(col.get(key))){
                         recheckGasValue.setDiffS(entry.get(key));
                     }
               }
              values.add(recheckGasValue);
        }
        recheckGasDrift.setRecheckGasValue(values);

    }
    public static void getFootData(List<Map<Integer,String>> list,RecheckGasDrift recheckGasDrift){
        RecheckGasFoot foot=new RecheckGasFoot();
        for (int i=0;i<list.size();i++){
            Map<Integer,String> entry= list.get(i);
            for (Integer key : entry.keySet()){
                if (RecheckConstant.ZERO_DRIFT_ABSOLUTE_ERROR.equals(entry.get(key))){
                    foot.setZeroDriftAbsoluteError(entry.get(key+2));
                }
                if (RecheckConstant.RANGE_DRIFT_ABSOLUTE_ERROR.equals(entry.get(key))){
                    foot.setRangeDriftAbsoluteError(entry.get(key+2));
                }
                if (RecheckConstant.ZERO_DRIFT.equals(entry.get(key))){
                    foot.setZeroDrift(entry.get(key+2));
                }
                if (RecheckConstant.SPAN_DRIFT.equals(entry.get(key))){
                    foot.setSpanDrift(entry.get(key+2));
                }

            }
        }
        recheckGasDrift.setRecheckGasFoot(foot);
    }



}
